<?php
/**
 * 
 * @author chenqi
 * @uses 用户
 * @date 2015-10-20
 *
 */
class Offer extends WX_Controller{
   
   
   public function __construct()
	{
		parent::__construct();
		$this->load->model("quote_model");
	}
	//个人中心
	//create by chenqi
	public function index() {	
		$this->display('/wap/offer');
	}

	//我的车辆的报价信息
	//create by chenqi
	public function info() {
		$vehicle_id = $this->input->get("id");//车辆id
		//判断车辆是否是此会员的
		$this->load->model("vehicle_model");
		$vehicle_info = $this->vehicle_model->checkVehicle(array("user_id"=>$this->user['id'],"vehicle.id"=>$vehicle_id));
		if(!$vehicle_info) {
			$go_url = "http://".$_SERVER['HTTP_HOST']."/wap/home/";
			$msg = "车辆信息错误";
			$this->location_href($msg, $go_url);
		}
		//判断此车辆是否有过订单
		$this->load->model("appoint_model");
		$check_order = $this->appoint_model->checkAppoint(array("vehicle_id" => $vehicle_id,"data_status" => 1));
		if($check_order) {
			$go_url = "http://".$_SERVER['HTTP_HOST']."/wap/user/order_det?order_id=".$check_order['id'];	
			echo "<script>window.location.replace('$go_url');</script>";
			exit;			
		}		
		$vehicles = $this->quote_model->getQuoteList(array("quote.vehicle_id"=>$vehicle_id));

		$this->load->model("company_model");
		$company_lists = array();
		if($vehicles){
			foreach ($vehicles as $key => $vehicle) {
				if($vehicle['quote_data']){
					$vehicle['offer'] = unserialize($vehicle['offer']);
					$vehicle['quote_data'] = unserialize($vehicle['quote_data']);//保险数据
					
					$company = $vehicle['quote_data']['company_id'];//保险公司
					$company_list = array();
					foreach ($company as $k => $val) {
						$cp = $this->company_model->checkInsure(array("id"=>$val));
						$company_list[$k] = $cp;
						$tmp = 0;
						foreach ($vehicle['quote_data']['sum_id'][$k] as $value) {
							$tmp +=$value;
						}//保险公司报价
						$company_list[$k]['offer_sum'] = $tmp;
						$company_list[$k]['coupon'] = $vehicle['coupon'];
					}				
					$company_lists[$vehicle['id']] =  $company_list;
				}
			}
		}
		$this->assign("car_id",$vehicle_id);
		$this->assign("company_lists",$company_lists);
		$this->display('/wap/offer_info');
	}
	//险种详情 我的车辆的不同险种的报价
	public function quote(){
		$q_id = $this->input->get("q_id");//报价id
		$insure_id = $this->input->get("id");//险种id
		$vehicle = $this->quote_model->checkQuote(array("quote.id"=>$q_id));
		$vehicle_id = $vehicle['vehicle_id']; //车辆id
		//判断车辆是否是此会员的
		$this->load->model("vehicle_model");
		$vehicle_info = $this->vehicle_model->checkVehicle(array("user_id"=>$this->user['id'],"vehicle.id"=>$vehicle_id));
		if(!$vehicle_info) {
			$go_url = "http://".$_SERVER['HTTP_HOST']."/wap/home";
			$msg = "车辆信息错误";
			$this->location_href($msg, $go_url);
		}
		//判断是否有此报价方案
		$quote_offer = unserialize($vehicle['quote_data']);
		$count = count($quote_offer['company_id']);
		if($count -1 < $insure_id) {
			$go_url = "http://".$_SERVER['HTTP_HOST']."/wap/home";
			$msg = "报价信息错误";
			$this->location_href($msg, $go_url);			
		}
		
		$vehicle['quote_data'] = unserialize($vehicle['quote_data']);//保险数据
		$company = $vehicle['quote_data']['company_id'][$insure_id];//保险公司
		$this->load->model("company_model");
		$cp = $this->company_model->checkInsure(array("id"=>$company));

		$price = $vehicle['quote_data']['sum_id'][$insure_id];//险种价钱
		$insurance_id = $vehicle['quote_data']['insurance_id'];//险种

		$this->load->model("insurance_model");//险种模型
		$insurance = array();
		foreach ($insurance_id as $k => $v) {
			$insurance[$k] = $this->insurance_model->checkInsurance(array("id"=>$v));
			$insurance[$k]['price'] = $price[$k];
		}
		$coupon =$vehicle['coupon'];
		$sum = array_sum($price)-$coupon;
		$this->assign("sum",$sum);
		$this->assign("coupon",$coupon);
		$this->assign("insure",$insurance);
		$this->assign("cp",$cp);

		$this->display("/wap/offer_detail");
	}
}